<template>
  <div class="addBrand-container">
    <div class="container">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="ruleForm.sex" placeholder="请选择性别">
            <el-option label="男" value="男"></el-option>   
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="image">
          <el-upload
            :limit="1"
            list-type="picture-card"
            class="avatar-uploader"
            action="/api/api/common/upload"
            :on-success="handleAvatarSuccess"
            :show-file-list="true"
            :headers="{token:token}"
            >
            <img v-if="ruleForm.image" :src="showImage" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <div class="subBox">
          <el-button type="primary" @click="submitForm()">保存</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import {selectUserById,updateUserById} from "@/api/login-register";
import Cookies from 'js-cookie';
export default {
  name: 'PersonalCenter',
  data() {
    return {
      dialogVisible:true,
      token:Cookies.get('token'),
      ruleForm:{
        id:'',
        username:'',
        image:'',
        sex:'',
        password:''
      },
      rules: {
        username: [
            { required: true, message: '请输入用户姓名', trigger: 'blur' }
        ]
      }
    };
  },

  mounted() {
    const id = Cookies.get('id');
    selectUserById(id).then((res)=>{
      this.ruleForm = res.data.data
      console.log("ruleForm",this.ruleForm)
    },(err)=>{
      alert(err.message)
    })
  },

  methods: {
    submitForm(){
      //进行表单校验
      this.$refs.ruleForm.validate((valid) => {
        if(valid) {
          updateUserById(this.ruleForm).then((res)=>{
          console.log("res.data",res.data)
          if(res.data.code === 200){
            this.$router.push({
              name: "layout",
            });
          }
        },(err)=>{
          alert(err.message)
        })
        }
      })
    },
    handleAvatarSuccess(res,file){
      // 图片上传成功修改头像
      Cookies.set('imageUrl',res.data)
      this.$store.commit('setImage',res.data)
      this.ruleForm.image = res.data
    }
  },
  computed:{
    showImage(){
      return Cookies.get('imageUrl')
    }
  }
};
</script>

<style lang="less" scoped>
.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .addBrand {
  &-container {
    margin: 30px;
    margin-top: 30px;
    .HeadLable {
      background-color: transparent;
      margin-bottom: 0px;
      padding-left: 0px;
    }
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;
      // min-height: 500px;
      .subBox {
        padding-top: 30px;
        text-align: center;
        border-top: solid 1px #eca712;
      }
    }
    .idNumber {
      margin-bottom: 39px;
    }

    .el-form-item {
      margin-bottom: 29px;
    }
    .el-input {
      width: 293px;
    }
  }
}
</style>